package com.ocgs.service.impl;

import com.ocgs.base.core.exception.BaseDAOException;
import com.ocgs.base.core.exception.BaseServiceException;
import com.ocgs.dao.impl.ProductDAOImpl;
import com.ocgs.model.Product;
import com.ocgs.pagination.PaginatedListImpl;
import com.ocgs.pagination.page.PageContext;
import com.ocgs.service.ProductService;
import org.displaytag.pagination.PaginatedList;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bryant
 * Date: 10/21/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductServiceImpl implements ProductService {

    private ProductDAOImpl productDAO;


    public int deleteByPrimaryKey(Integer id) throws BaseServiceException {
       try{
           return productDAO.deleteByPrimaryKey(id);
       }catch (BaseDAOException e){
           throw new BaseServiceException(e);
       }

    }

    public int insert(Product record) throws BaseServiceException{
        try{
            return productDAO.insert(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }
    public int insertSelective(Product record) throws BaseServiceException {
        try{
            return productDAO.insert(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    
    public Product selectByPrimaryKey(Integer id) throws BaseServiceException {
        try{
            return productDAO.selectByPrimaryKey(id);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    
    public int updateByPrimaryKeySelective(Product record) throws BaseServiceException {
        try{
            return productDAO.updateByPrimaryKeySelective(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Product record) throws BaseServiceException {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }


    public int updateByPrimaryKey(Product record) throws BaseServiceException {
        try{
            return productDAO.updateByPrimaryKey(record);
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public void setProductDAO(ProductDAOImpl productDAO) throws BaseServiceException {
        this.productDAO = productDAO;
    }

    public ProductDAOImpl getProductDAO() throws BaseServiceException {
        return productDAO;
    }

    public PaginatedList findAllProductForPagination(PaginatedListImpl paginatedList,Product product) throws BaseServiceException{

        try{
            PageContext page = PageContext.getPageContext();
            List<Product> products = productDAO.findAllProduct(product);
            if(products==null||products.size()==0)
                return null;
            paginatedList.setList(products);
            paginatedList.setTotalNumberOfRows(page.getTotalRows());
            return  paginatedList;
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public List<Product> findAllProduct() throws BaseServiceException {
        try{
           return productDAO.findAllProduct(new Product());
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public List<Product> getAllProduct() throws BaseServiceException{
        try{
            return productDAO.getAllProduct();
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }

    public PaginatedList findAllStorageForPagination(PaginatedListImpl paginatedList,Product product) throws BaseServiceException{
        try{
            PageContext page = PageContext.getPageContext();
            List<Product> products = productDAO.searchStorage(product);
            if(products==null||products.size()==0)
                return null;
            paginatedList.setList(products);
            paginatedList.setTotalNumberOfRows(page.getTotalRows());
            return  paginatedList;
        }catch (BaseDAOException e){
            throw new BaseServiceException(e);
        }
    }
}
